package entity_controllers.options.effects;

import core.subcontrollers.PlayerController;
import boundary.UI;
import ie.FieldBoard;
import ie.PlayerBoard;
import ie.Shaker;
import dto.Player;
import entity.Account;
import entity_controllers.Option;
import entity_controllers.fieldcontrollers.ChanceController;
import entity_controllers.options.EndTurnOption;
import entity_controllers.options.ShakeMoveOption;

public class JailEffect extends Option {

	private static final int jailAmount = 1000;
	private int turnsInJail;
	private int jailCards;
	
	public JailEffect(String title) {
		super("");
	}

	@Override
	public void execute(Player player, FieldBoard fieldBoard, PlayerBoard playerBoard, Shaker shaker) {
	
		Account account = (Account) player.getAccount();
		String[] actions;

		if (jailCards > 0){
			actions = new String[]{"Kast", "Betal "+jailAmount, "Brug benådningskort"};
		}
		
		else {
			actions = new String[]{"Kast", "Betal "+jailAmount};
		}
		
		String action = UI.get().getBooleanMultiple("Choose one", actions);

		// Prompt player if he wants to roll or pay
		// The option to use a "get out from jail option" appears if the player has one or more of the card.
		switch (action) {
			case "Kast":
				shaker.shake();
				UI.get().showDice(shaker.getDices());
				
				if(shaker.getDices()[0].getValue() == shaker.getDices()[1].getValue()) {
					UI.get().getMessage("Du slog to ens og kan nu rykke ud af fængslet");
					outOfJail(account);
					player.setDiceSum(shaker.getDiceSum());
					PlayerController.movePlayerByDiceSum(player, fieldBoard, playerBoard, shaker);
					account.getOM().getOption(ShakeMoveOption.class).enable();
					
				} else {
					turnsInJail++;
					
					// Removes the player from jail if it is his fourth turn in jail
					if (turnsInJail == 3) {
						UI.get().getMessage("Du betaler bøden på 1000 og rykker ud næste gang");
						payJailAmount(account);
					}
					
					account.getOM().getOption(EndTurnOption.class).enable();
				}
				break;
				
			case "Betal "+jailAmount:
				UI.get().getMessage("Du betaler bøden på 1000 og kan rykke ud");
				payJailAmount(account);
				account.getOM().getOption(ShakeMoveOption.class).enable();
				
				break;
	
			case "Brug benådningskort":
				UI.get().getMessage("Du bruger dit benådningskort og kan rykke ud");
				account.getOM().getOption(ShakeMoveOption.class).enable();
				removeJailCard();
					
			default:
				break;
		}
		
	}
	
	private void payJailAmount(Account account) {
		account.subBalance(jailAmount);
		outOfJail(account);
	}
	
	private void outOfJail(Account account) {
		this.disable();
		account.getEM().getOption(MoveEffect.class).enable();
		turnsInJail = 0;
	}

	@Override
	public void reset() {
		turnsInJail = 0;
		this.jailCards = 0;
	}

	public void addJailCard(){
		this.jailCards++;
	}
	
	private void removeJailCard(){
		ChanceController.putBackCard(); // temporary fix for putting back JailCard in the deck.
		this.jailCards--;
	}
	
	public int getJailCards(){
		return this.jailCards;
	}

}
